#include "../import/head.h"
#include "protocol_gate_stock_list.h"
#include "../data_struct/head.h"
#include "../global/head.h"
#include "../macro/head.h"
#include "../msg/head.h"

namespace xos_sgsj
{

    protocol_gate_stock_list::protocol_gate_stock_list()
    {
    }

    protocol_gate_stock_list::~protocol_gate_stock_list()
    {
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int protocol_gate_stock_list::post_for_stock_list( session * pSession )
    {
        int ret = 0;

        xos::i_buf * pbuf = 0;

        g_pSysData->m_os.m_pXos->create( xos::i_xos::XOS_OBJ_BUF, ( void ** ) &pbuf );

        g_pSysData->m_os.m_pSerial->pack_int16( pbuf, PROTOCOL_GATE_STOCK_LIST );
        
        xos::i_msg * p = 0;
        ret = g_pSysData->m_os.m_pXos->create( xos::i_xos::XOS_OBJ_MSG, ( void ** ) &p );
        p->set_int( 0, xos_package::MSG_COMPOSE_PACKAGE );
        p->set_int( 1, 0 );
        p->set_bool( 1, false );
        p->set_bool( 2, false );
        p->set_buf( 0, pbuf );
        p->set_void( 0, pSession );
        p->set_void( 1, pSession->m_pEncrypt );
        pSession->add_ref( 1 );
        g_pSysData->m_os.m_pPackage->request( p );
        pbuf = 0;
        p = 0;

        return ret;
    }

    int protocol_gate_stock_list::proc( xos::i_buf * pBuf, session * pSession )
    {
        int ret = 0;

        int num = 0;

        if( 0 == ret )
        {
            LOG4( "recv stock list" );
        }

        if( 0 == ret )
        {
            xos::xos_u16 u16 = 0;
            ret = g_pSysData->m_os.m_pSerial->un_pack_int16( pBuf, &u16 );
            num = ( int )u16;
        }

        for( int i = 0; ( 0 == ret ) && ( i < num ); ++i )
        {
            xos::xos_u16 u16 = 0;
            ret = g_pSysData->m_os.m_pSerial->un_pack_int16( pBuf, &u16 );
            session::SESSION_MAP_ITER it = g_pSysData->m_pPool->m_stock_map.find( u16 );
            if( it != g_pSysData->m_pPool->m_stock_map.end() )
            {
                continue;
            }
            g_pSysData->m_pPool->m_stock_map[u16] = pSession;
            pSession->m_stock_list.push_back( u16 );
        }

        return ret;
    }
    
} // xos_sgsj
